package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.Product;

import lombok.Data;

@Data
public class ProductDto {
	private static String PRODUCT_URL = "http://img.taobaocdn.com/bao/uploaded";

	@JsonProperty("product_id")
	private long productID;
	@JsonProperty("product_name")
	private String productName;
	@JsonProperty("product_url")
	private String pictureUrl;
	@JsonProperty("category_id")
	private long categoryID;
	@JsonProperty("user_id")
	private long userID;

	@JsonProperty("sales_count")
	private int sales;
	private int quantity; // 库存
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
	@JsonProperty("publish_time")
	private Date publishTime; // 上架时间

	private int price;// 单位元

	private AdgroupDto adgroup;

	@Data
	@JsonInclude(Include.NON_DEFAULT)
	public static class AdgroupDto {
		@JsonProperty("adgroup_id")
		private long adgroupID;
		@JsonProperty("adgroup_url")
		private String adgroupUrl;
		@JsonProperty("adgroup_name")
		private String adgroupName;

		public void from(Adgroup adgroup) {
			this.adgroupID = adgroup.getAutoID();
			this.adgroupUrl = adgroup.getProductUrl();
			this.adgroupName = adgroup.getProductName();
		}
	}

	public void from(Product po) {
		BeanUtils.copyProperties(po, this);
		if (!po.getPictureUrl().contains(PRODUCT_URL)) {
			this.pictureUrl = PRODUCT_URL + po.getPictureUrl();
		}
	}

	public Product to() {
		Product po = new Product();
		BeanUtils.copyProperties(this, po);
		return po;

	}
}
